using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using NBear.Common;

namespace NBear.Test.UnitTests
{
    public interface EntityWithCustomProperty : IEntity
    {
        [PrimaryKey]
        int ID { get; }
        string Name { get; set; }

        string XmlServerConfig { get; set; }

        [CustomProperty]
        SampleCustomPropertyType SampleProperty { get; }

        string XmlContactConfig { get; set; }
    }

    [Serializable]
    public class SampleServerConfig
    {
        public string ServerAddress;
        public int ServerPort;
    }

    [Serializable]
    public class SampleContactConfig
    {
        public string WorkPhone;
        public string HomePhone;
    }

    public class SampleCustomPropertyType : CustomPropertyType
    {
        public SampleServerConfig ServerConfig
        {
            get
            {
                return SerializeHelper.Deserialize<SampleServerConfig>(typeof(SampleServerConfig), (string)keyValues["XmlServerConfig"]);
            }
        }

        public SampleContactConfig ContactConfig
        {
            get
            {
                return SerializeHelper.Deserialize<SampleContactConfig>(typeof(SampleContactConfig), (string)keyValues["XmlContactConfig"]);
            }
        }

        public SampleCustomPropertyType(KeyValueCollection keyValues, string[] parameters)
            : base(keyValues, parameters)
        {
        }
    }

    /// <summary>
    /// Summary description for TestCustomProperty
    /// </summary>
    [TestClass]
    public class TestCustomProperty
    {
        public TestCustomProperty()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestCustomPropertyMethod()
        {
            //provided the entity obj's value is read from database
            EntityWithCustomProperty obj = EntityFactory<EntityWithCustomProperty>.CreateObject();
            SampleServerConfig sc = new SampleServerConfig();
            sc.ServerAddress = "127.0.0.1";
            sc.ServerPort = 8888;
            obj.XmlServerConfig = SerializeHelper.Serialize(sc);
            SampleContactConfig cc = new SampleContactConfig();
            cc.WorkPhone = "110";
            cc.HomePhone = "119";
            obj.XmlContactConfig = SerializeHelper.Serialize(cc);

            //now we can use Custom Property like following
            Assert.AreEqual(obj.SampleProperty.ServerConfig.ServerAddress, "127.0.0.1");
            Assert.AreEqual(obj.SampleProperty.ServerConfig.ServerPort, 8888);
            Assert.AreEqual(obj.SampleProperty.ContactConfig.WorkPhone, "110");
            Assert.AreEqual(obj.SampleProperty.ContactConfig.HomePhone, "119");
        }
    }
}
